package com.it.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.it.reggie.common.BaseContext;
import com.it.reggie.common.R;
import com.it.reggie.entity.ShoppingCart;
import com.it.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //添加菜品和套餐到购物车
    @PostMapping("/add")   //因为是json形式数据
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);
        //设置用户id，指定当前是哪个用户的购物车数据(用户登录成功之后，id通过session存起来了,通过BaseContext也能获得)
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);//设置到购物车里面去
        //查询当前菜品(dishId)或者套餐(setmealId)是否在购物车中
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);//把用户id作为条件查询

        if(dishId != null){
            //添加到购物车的是菜品//动态的添加dishId
            queryWrapper.eq(ShoppingCart::getDishId,dishId);//dishId也是通过shoppingCart.getDishId()获取到的
        }else{
            //添加到购物车的是套餐//动态的添加setmealId
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //SQL: select * from shopping_cart where user_id = ? and dish_id/setmeal_id = ?    只能查出来唯一的一条数据
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if(cartServiceOne!=null){
            //如果已存在，就number+1
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //如果不存在，默认number=1
            //前端页面提交shoppingcart过来的时候并没有把number传过来，
            //只传了amount:498 dishFlavor:"不要香菜" dishId:"1397862198033297410" image:"583df4b7-a159-4cfc-9543-4f666120b25f.jpeg" name:"老火靓汤"
            //所以要手动设置number值
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());//设置入库时间
            shoppingCartService.save(shoppingCart);
            cartServiceOne = shoppingCart;//重新覆盖cartServiceOne(要用来返回)，因为进入的是else分支,所以cartServiceOne=null
        }

        return R.success(cartServiceOne);
    }

    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车.。。。");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());//以用户id为查询条件
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);//在入库时设计了创建时间

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);//

        return R.success(list);
    }
    //清空购物车
    @DeleteMapping("/clean")
    public R<String> clean(){
        //SQL:delete from shopping_cart where user_id = ?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);

        return R.success("清空购物车成功");
    }

}
